<?php

namespace App\Entity\Machine;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Base\Annotation\EntityInfo;
use App\Repository\Machine\MachineGroupRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @EntityInfo(name="MachineGroup",cnName="设备标记")
 * @ApiResource()
 * @ORM\Entity(repositoryClass=MachineGroupRepository::class)
 */
class MachineGroup
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"group"})
     */
    private $id;

    /**
     * @Common(cnName="名称",listType="text",editType="text",width="300")
     * @ORM\Column(type="string", length=255)
     * @Groups({"group"})
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity=Machine::class, mappedBy="groups", orphanRemoval=true)
     */
    private $machines;

    public function __construct()
    {
        $this->machines = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Machine[]
     */
    public function getMachines(): Collection
    {
        return $this->machines;
    }

    public function addMachine(Machine $Machine): self
    {
        if (!$this->machines->contains($Machine)) {
            $this->machines[] = $Machine;
            $Machine->setGroups($this);
        }

        return $this;
    }

    public function removeMachine(Machine $machine): self
    {
        if ($this->machines->contains($machine)) {
            $this->machines->removeElement($machine);
            // set the owning side to null (unless already changed)
            if ($machine->getGroups() === $this) {
                $machine->setGroups(null);
            }
        }

        return $this;
    }
}
